package com.kunbo.cn.controller.core.poundlist;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kunbo.cn.entity.core.error.ExceptionBean;
import com.kunbo.cn.entity.core.poundlist.PurchasePoundList;
import com.kunbo.cn.entity.system.BusinessCode;
import com.kunbo.cn.enumeration.core.poundlist.PoundListState;
import com.kunbo.cn.service.core.poundlist.PurchasePoundListService;
import com.kunbo.cn.service.system.BusinessCodeService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.BuildBean1;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.Pages;
import com.kunbo.cn.util.retu.ResultBean;
import com.kunbo.cn.util.retu.ResultBean1;
/**
 * 采购磅单
 * @author  编写人:刘义
 * @date 时间:2019年3月18日上午11:24:29
 */
@RestController
@RequestMapping("/ppl")
public class PurchasePoundListController {
	@Autowired
	private PurchasePoundListService pplService;
	
	@Autowired
	private BusinessCodeService businessCodeService;
	/**
	 * 
	 * <p>Title: add</p>
	 * <p>Description:采购磅单首次过磅 </p>
	 * @param type
	 * @param jsonstr
	 * @return
	 */
//	@RequestMapping(value="/add",method=RequestMethod.POST)
//	public CommonResult add(String type,String jsonstr) {
////		String type = request.getParameter("type");
////		String jsonStr = request.getParameter("jsonstr");
//		if(StringUtils.isBlank(type)) {
//			return new CommonResult(false, "类型不可为空", null);
//		}else if(StringUtils.isBlank(jsonstr)) {
//			return new CommonResult(false, "json串为空", null);
//		}
//		try {
//			pplService.save(type,jsonstr);
//			return new CommonResult(true, "磅单新增成功", null);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return new CommonResult(false, "异常导致新增磅单失败", null);
//			
//		}
//
//	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public CommonResult add(String noticeCode,Double weight) {
//		String type = request.getParameter("type");
//		String jsonStr = request.getParameter("jsonstr");
		if(StringUtils.isBlank(noticeCode)) {
			ExceptionBean error = new ExceptionBean();
			error.setSpeakMsg("通知单号为空");
			error.setScreenLine1("");
			error.setScreenLine2("");
			return new CommonResult(false, "", error);
		}else if(null==weight) {
			ExceptionBean error = new ExceptionBean();
			error.setSpeakMsg("称重数据为空");
			error.setScreenLine1("");
			error.setScreenLine2("");
			return new CommonResult(false, "", error);
		}
		try {
			Map<String,Object> map = pplService.save(noticeCode,weight);
			return new CommonResult(true, "磅单新增成功", map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ExceptionBean error = new ExceptionBean();
			error.setSpeakMsg("异常导致新增磅单失败");
			error.setScreenLine1("");
			error.setScreenLine2("");
			return new CommonResult(false, "", error);
			
		}

	}
	/**
	 * 
	 * <p>Title: update</p>
	 * <p>Description:采购磅单二次过磅 </p>
	 * @param type
	 * @param jsonstr
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public CommonResult update(String type,String jsonstr) {
		if(StringUtils.isBlank(type)) {
			return new CommonResult(false, "类型不可为空", null);
		}else if(StringUtils.isBlank(jsonstr)) {
			return new CommonResult(false, "json串为空", null);
		}
		try {
			Map<String,Object> rm = pplService.update(type,jsonstr);
			return new CommonResult(true, "称重完成,请下磅,"+rm.get("msg").toString(), rm.get("purchasePound"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new CommonResult(false, "异常导致新增磅单失败", null);
			
		}

	}
	
	/**
	 * 
	 * <p>Title: update</p>
	 * <p>Description:磅单补增 </p>
	 * @param type
	 * @param jsonstr
	 * @return
	 */
	@RequestMapping(value="/supplementaryIncrease",method=RequestMethod.POST)
	public CommonResult supplementaryIncrease(String jsonstr) {
		
		if(StringUtils.isBlank(jsonstr)) {
			return new CommonResult(false, "json串为空", null);
		}
		try {
			pplService.supplementaryIncrease(jsonstr);
			return new CommonResult(true, "磅单更新成功", null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new CommonResult(false, "异常导致新增磅单失败", null);
			
		}

	}
	
	/**
	 * 选采购销售单号
	 * @param startTime  开始时间
	 * @param endTime    结束时间
	 * @param supplier   供应商
	 * @param material   物料
	 * @param orderCode  订单号
	 * @param page       当前页
	 * @param pageSize   当前页数据长度
	 * @return
	 */
	@PostMapping("/selOrderCode.do")
	public CommonResult selOrderCode(
			@RequestParam String startTime,
			@RequestParam String endTime,
			@RequestParam String supplier,
			@RequestParam String material,
			@RequestParam String orderCode,
			@RequestParam Integer page,
			@RequestParam Integer pageSize
			) {
		return pplService.selOrderCode(startTime,endTime,supplier,material,orderCode,page,pageSize);
	}
	
	
	
	/**
	 * 级联显示采购补增单其他信息
	 * @param orderCode  发货单号
	 * @return
	 */
	@PostMapping("/showOhterMes")
	public CommonResult showOhterMes(@RequestParam String orderCode) {
		return pplService.showOhterMes(orderCode);
	}
	
	
	
	
	
	
	/**
	 * <p>Title: update</p>
	 * <p>Description:根据身份证号获取磅单 </p>
	 * @param idNumber
	 * @return
	 */
	@RequestMapping(value="/findByIdNumber",method=RequestMethod.POST)
	public CommonResult update(String idNumber) {
		if (StringUtils.isBlank(idNumber)) {
			return new CommonResult(false, "参数为空", null);
		}
		try {
			PurchasePoundList ppl = pplService.findByIDNumber(idNumber,PoundListState.firstWeigh);
			return new CommonResult(true, "获取成功", ppl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new CommonResult(false, "异常导致获取失败", null);
			
		}

	}
	
	
	
	@RequestMapping("/sel")
	public ResultBean<PurchasePoundList> sel(
			@RequestParam("idCard")String idCard
			){
		ResultBean<PurchasePoundList> result  = new ResultBean<PurchasePoundList>();
		return BuildBean.buildResult(result, "success", "error", pplService.sel(idCard));
	}
	/**
	 * 
	 * <p>Title: findByPage</p>
	 * <p>Description:动态查询采购磅单信息 </p>
	 * @param ppl
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getPurchasePoundList")
	public Page<PurchasePoundList> findPplByPage(@RequestBody(required=false) PurchasePoundList ppl,
			@RequestParam(required=false) String start_time,
			@RequestParam(required=false) String end_time,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer pageSize ){
		if(pageNum == null){
			pageNum = 0;
		}
		if(pageSize == null){
			pageSize = 10;
		}
		return pplService.findByPplPage(ppl, pageNum, pageSize);
	}
	/*@PostMapping("/PoundList")
	public ResultBean1<List<Map<String,Object>>> PoundList(
			@RequestParam String start_time,
			@RequestParam String end_time,
			@RequestParam String  supplier,//供应商  
			@RequestParam String cgPoundNumber,//过磅单号 
			@RequestParam String netWeight,//净重
			@RequestParam String receivingUnit,//收货单位 
			@RequestParam String arrivalOfGoodsNumber,//到货通知单号
			@RequestParam String matter_type,//物料类型
			@RequestParam String materiel,//物料
			@RequestParam String wagonNumber,//车号
			@RequestParam Integer page,
			@RequestParam Integer pageSize
			){
		ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
		ResultBean1<List<Map<String,Object>>> result1 = new ResultBean1<List<Map<String,Object>>>();
		ResultBean<List<Map<String,Object>>>njks=BuildBean.buildResult(result, "success", "error", pplService.PoundList(start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,page,pageSize));//id
		int total=njks.getData().size();
		return BuildBean1.buildResult(result1, "success", "error",total,pplService.PoundList(start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,page,pageSize));//id
    }*/
	@PostMapping("/PoundList")
	public ResultBean1<Pages> PoundList(
			@RequestParam String start_time,
			@RequestParam String end_time,
			@RequestParam String  supplier,//供应商  
			@RequestParam String cgPoundNumber,//过磅单号 
			@RequestParam String netWeight,//净重
			@RequestParam String receivingUnit,//收货单位 
			@RequestParam String arrivalOfGoodsNumber,//到货通知单号
			@RequestParam String matter_type,//物料类型
			@RequestParam String materiel,//物料
			@RequestParam String wagonNumber,//车号
			@RequestParam Integer page,
			@RequestParam Integer pageSize
			){
		ResultBean1<Pages> result = new ResultBean1<Pages>();
		return BuildBean1.buildResulttoPages(result, "success", "error",pplService.PoundList1(start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,page,pageSize));//id
    }

	@PostMapping("/PoundListExport")
	public Object[][] PoundListExport(
			@RequestParam String start_time,
			@RequestParam String end_time,
			@RequestParam String  supplier,//供应商  
			@RequestParam String cgPoundNumber,//过磅单号 
			@RequestParam String netWeight,//净重
			@RequestParam String receivingUnit,//收货单位 
			@RequestParam String arrivalOfGoodsNumber,//到货通知单号
			@RequestParam String matter_type,//物料类型
			@RequestParam String materiel,//物料
			@RequestParam String wagonNumber//车号
			){//start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,page,pageSize
		return pplService.PoundListExport(start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber);//id
    }
	//新采购磅单导出
	@RequestMapping(value="PoundListExports",method=RequestMethod.GET)
	public void PoundListExports(
			@RequestParam String start_time,
			@RequestParam String end_time,
			@RequestParam String supplier,//供应商  
			@RequestParam String cgPoundNumber,//过磅单号 
			@RequestParam String netWeight,//净重
			@RequestParam String receivingUnit,//收货单位 
			@RequestParam String arrivalOfGoodsNumber,//到货通知单号
			@RequestParam String matter_type,//物料类型
			@RequestParam String materiel,//物料
			@RequestParam String wagonNumber,//车号,
			HttpServletResponse response
			){//start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,page,pageSize
		 pplService.PoundListExports(start_time,end_time,supplier,cgPoundNumber,netWeight,receivingUnit,arrivalOfGoodsNumber,matter_type,materiel,wagonNumber,response);//id
    }
	@PostMapping("/findOne")
	public ResultBean<List<Map<String,Object>>>findOne(
			@RequestParam String uuid
			){
		ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
		return BuildBean.buildResult(result, "success", "error",pplService.findOne(uuid));//id
	}
	
	/**
	 * 磅单补增获取内码
	 */
	@RequestMapping("/getPoundNumber")
	public String findPplByPage(String type){
		//1.先判断有没有更新对应的日期和内码
		String date = "" ;
		List<BusinessCode> list = businessCodeService.findAll();
		if(list.size()>0) {
			date = businessCodeService.findAll().get(0).getDate();
		}
		if(!date.equals(SDate.getSystemDateYMDToString())) {
			businessCodeService.updateNumberAndDate(1, SDate.getSystemDateYMDToString());
		}
		//根据类型和日期获取对应的的 内码
		Integer n = 1;
		String nStr = "";
		BusinessCode b = businessCodeService.findByTypeAndDate(type, SDate.getSystemDateYMDToString());
		if(null==b) {
			return (""+System.currentTimeMillis()).substring(0, 5);
		}else {
			n = b.getNumber();
			
		}
		if(n>0 && n<10) {
			nStr ="000" +n;
		}else if(n>=10 && n<100) {
			nStr ="00" +n;
		}else if(n>=100 && n<1000) {
			nStr ="0" +n;
		}else {
			nStr ="" +n;
		}
		return type+SDate.getSystemDateYMD2ToString()+nStr;
		
	}
	/**
	 * 获取组织管理
	 */
	@PostMapping("/getNoticeInfo")
	public ResultBean<List<Map<String,Object>>> getNoticeInfo(){
		ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
		return BuildBean.buildResult(result, "succes", "error",pplService.getNoticeInfo());
	}
	/**
	 * 获取所有司机
	 * @return
	 */
	/*@PostMapping("/carDrivers")
	public ResultBean<List<Map<String,Object>>> carDrivers(
			){
		ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
		return BuildBean.buildResult(result, "succes", "error",service.carDrivers());
	}
*/
}
